<?php


namespace App\Helpers\Traits\Models;


use Carbon\Carbon;
use Illuminate\Database\Eloquent\Builder;

trait TimeScope {
    public function scopeCreatedAtOfDay(Builder $query, $data) {
        return $query->whereBetween('created_at',
            [Carbon::parse($data)->startOfDay(), Carbon::parse($data)->endOfDay()]);
    }

    public function scopeCreatedAtBefore(Builder $query, $date): Builder {
        return $query->where('created_at', '<=', Carbon::parse($date));
    }

    public function scopeCreatedAtAfter(Builder $query, $date): Builder {
        return $query->where('created_at', '>=', Carbon::parse($date));
    }

    public function scopeCreatedAtBetween(Builder $query, $start, $end): Builder {

        return $query->whereBetween('created_at', [$start, $end]);
    }

    public function scopeUpdatedAtBefore(Builder $query, $date): Builder {
        return $query->where('updated_at', '<=', Carbon::parse($date));
    }

    public function scopeUpdatedAtAfter(Builder $query, $date): Builder {
        return $query->where('updated_at', '>=', Carbon::parse($date));
    }

    public function scopeUpdatedAtBetween(Builder $query, $start, $end): Builder {
        return $query->whereBetween('updated_at', [$start, $end]);
    }

    public function scopeLastLoginTimeBefore(Builder $query, $date): Builder {
        return $query->where('last_login_time', '<=', Carbon::parse($date));
    }

    public function scopeLastLoginTimeAfter(Builder $query, $date): Builder {
        return $query->where('last_login_time', '>=', Carbon::parse($date));
    }

    public function scopeLastLoginTimeBetween(Builder $query, $start, $end): Builder {
        return $query->whereBetween('last_login_time', [$start, $end]);
    }
}
